<script setup lang="ts">
import {computed, onMounted, ref} from "vue";
import {Top} from "@element-plus/icons-vue";
import {commonStore} from '@/store/common.ts'
import {fetchFooterTag} from "@/request/req.ts";

// 滚动到顶部
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  });
};

const common = commonStore()

const ShopPhone = computed(() => {
  const item = common.getItemById(2)
  return item?.value
})

const UnifyNumber = computed(() => {
  const item = common.getItemById(4)
  return item?.value
})
/**
 * 获取页脚标签
 */
const getFetchFooterTag = async () => {
  try {
    const res = await fetchFooterTag()
    if (res && Array.isArray(res)) {
      // 根据TypeName分组数据
      const groupedData = res.reduce((acc, item) => {
        const typeName = item.TypeName
        if (!acc[typeName]) {
          acc[typeName] = []
        }
        acc[typeName].push({
          name: item.Title,
          path: '/message',
          query: {id: item.Id}
        })
        return acc
      }, {})

      menuSections.value = Object.entries(groupedData)
          .map(([typeName, items]) => ({
            title: typeName,
            items: items as MenuItem[]
          }))

      // 更新客服信息
      const serviceId = groupedData['其他']?.[0]?.query?.id || 21
      serviceInfo.value = {
        name: `客服專線：${ShopPhone.value || 'undefined'}`,
        Week: '週一到週日10:00-22:00',
        path: '/message',
        query: {id: serviceId}
      }
    }
  } catch (error) {
    console.error('获取页脚标签失败:', error)
  }
}

// 定义菜单项类型
interface MenuItem {
  name: string
  path: string
  query: { id: number }
}

interface MenuSection {
  title: string
  items: MenuItem[]
}

interface ServiceItem {
  name: string
  Week: string
  path: string
  query: { id: number }
}

// 初始化空的menuItems，将由API数据填充
const menuSections = ref<MenuSection[]>([])
const serviceInfo = ref<ServiceItem>({
  name: `客服專線：${ShopPhone.value || 'undefined'}`,
  Week: '週一到週日10:00-22:00',
  path: '/message',
  query: {id: 21}
});

onMounted(() => {
  getFetchFooterTag()
})
</script>
<template>
  <footer class="footer">
    <div class="footer-content">
      <div class="container">
        <div class="footer-sections">
          <div
              v-for="section in menuSections"
              :key="section.title"
              class="footer-section"
          >
            <h4 class="section-title">{{ section.title }}</h4>
            <div class="menu-list">
              <router-link
                  v-for="item in section.items"
                  :key="item.query.id"
                  :to="{path: item.path, query: item.query}"
                  class="menu-link"
                  @click="scrollToTop"
              >
                {{ item.name }}
              </router-link>
            </div>
          </div>
        </div>

        <div class="service-section">
          <div class="service-info">
            <h4 class="section-title">{{ serviceInfo.name }}</h4>
            <p class="service-time">{{ serviceInfo.Week }}</p>
            <router-link
                :to="{path: serviceInfo.path, query: serviceInfo.query}"
                class="contact-link"
                @click="scrollToTop"
            >
              聯係我們
            </router-link>
            <p style="color: var(--text-secondary);margin-top: var(--spacing-sm);">統編：{{ UnifyNumber }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 回到顶部按钮 -->
    <el-backtop :right="50" :bottom="50" class="custom-backtop" :behavior="'smooth'">
      <div class="back-to-top">
        <el-icon>
          <Top/>
        </el-icon>
      </div>
    </el-backtop>
  </footer>
</template>

<style scoped>
.footer {
  background: var(--gradient-bg-dark);
  border-top: 2px solid var(--border-primary);
  margin-top: auto;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
}

/* 服务与帮助 */
.footer-content {
  padding: var(--spacing-4xl) 0 var(--spacing-xl);
  background: var(--gradient-bg-primary);
}

.footer-sections {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--spacing-4xl);
  margin-bottom: var(--spacing-4xl);
}

.section-title {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
  margin-bottom: var(--spacing-lg);
  padding-bottom: var(--spacing-sm);
  border-bottom: 2px solid var(--primary-color);
  display: inline-block;
  background: linear-gradient(135deg, var(--primary-color), var(--success-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.menu-link {
  color: var(--text-secondary);
  text-decoration: none;
  font-size: var(--font-size-sm);
  transition: var(--transition-all);
  display: block;
  padding: var(--spacing-xs) 0;
  border-radius: var(--radius-sm);
}

.menu-link:hover {
  color: var(--primary-color);
  padding-left: var(--spacing-sm);
  background: var(--primary-bg);
}

/* 客服信息 */
.service-section {
  border-top: 1px solid var(--border-primary);
  padding-top: var(--spacing-xl);
}

.service-info {
  text-align: center;
}

.service-info .section-title {
  border: none;
  margin-bottom: var(--spacing-sm);
}

.service-time {
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  margin-bottom: var(--spacing-md);
}

.contact-link {
  display: inline-block;
  color: var(--primary-color);
  text-decoration: none;
  font-size: var(--font-size-sm);
  padding: var(--spacing-sm) var(--spacing-xl);
  border: 2px solid var(--primary-color);
  border-radius: var(--radius-lg);
  transition: var(--transition-all);
  font-weight: var(--font-weight-medium);
}

.contact-link:hover {
  background: var(--gradient-primary);
  color: var(--text-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow-medium);
}

/* 回到顶部 */
.custom-backtop :deep(.el-backtop) {
  border: none !important;
  box-shadow: none !important;
  background: transparent !important;
}

.back-to-top {
  width: var(--spacing-4xl);
  height: var(--spacing-4xl);
  background: var(--gradient-primary);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: var(--transition-all);
  border: none;
  box-shadow: var(--shadow-medium);
}

.back-to-top:hover {
  background: linear-gradient(135deg, var(--primary-hover), var(--primary-dark));
  transform: scale(1.1);
  box-shadow: var(--shadow-strong);
}

.back-to-top .el-icon {
  color: var(--text-light);
  font-size: var(--font-size-base);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .footer-sections {
    grid-template-columns: repeat(3, 1fr);
    gap: var(--spacing-3xl);
  }
}

@media (max-width: 768px) {
  .container {
    padding: 0 var(--spacing-md);
  }

  .footer-content {
    padding: var(--spacing-3xl) 0 var(--spacing-lg);
  }

  .footer-sections {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-xl);
    margin-bottom: var(--spacing-3xl);
  }

  .footer-section {
    margin-bottom: var(--spacing-xl);
  }

  .section-title {
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-md);
    padding-bottom: var(--spacing-xs);
  }

  .menu-list {
    gap: var(--spacing-sm);
  }

  .menu-link {
    font-size: var(--font-size-xs);
    line-height: 1.4;
  }

  .service-section {
    padding-top: var(--spacing-lg);
  }

  .service-info .section-title {
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-xs);
  }

  .service-time {
    font-size: var(--font-size-xs);
    margin-bottom: var(--spacing-sm);
  }

  .contact-link {
    font-size: var(--font-size-xs);
    padding: var(--spacing-xs) var(--spacing-lg);
  }

  .back-to-top {
    width: 36px;
    height: 36px;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 var(--spacing-sm);
  }

  .footer-content {
    padding: var(--spacing-xl) 0 var(--spacing-sm);
  }

  .footer-sections {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-lg);
    margin-bottom: var(--spacing-xl);
  }

  .footer-section {
    margin-bottom: var(--spacing-lg);
  }

  .section-title {
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-sm);
    padding-bottom: var(--spacing-xs);
  }

  .menu-list {
    gap: var(--spacing-sm);
  }

  .menu-link {
    font-size: var(--font-size-xs);
    line-height: 1.3;
  }

  .service-section {
    padding-top: var(--spacing-md);
  }

  .service-info .section-title {
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-xs);
  }

  .service-time {
    font-size: 11px;
    margin-bottom: var(--spacing-sm);
  }

  .contact-link {
    font-size: 11px;
    padding: var(--spacing-xs) var(--spacing-md);
  }

  .back-to-top {
    width: 32px;
    height: 32px;
  }
}

@media (max-width: 360px) {
  .container {
    padding: 0 var(--spacing-xs);
  }

  .footer-sections {
    grid-template-columns: 1fr;
    gap: var(--spacing-md);
  }

  .footer-section {
    margin-bottom: var(--spacing-md);
  }

  .section-title {
    font-size: var(--font-size-xs);
    margin-bottom: var(--spacing-sm);
  }

  .menu-link {
    font-size: 11px;
  }

  .service-info .section-title {
    font-size: var(--font-size-xs);
  }

  .service-time {
    font-size: 10px;
  }

  .contact-link {
    font-size: 10px;
    padding: var(--spacing-xs) var(--spacing-sm);
  }
}
</style>
